from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from gis.admin.decorators import require_login, check_permission
from gis.common.exceptions import BizException
from itpay.core.mch.errors import ERROR_MCH_NOT_EXISTS
from itpay.core.recharge_order import order_service, recharge_comment_service
from itpay.core.recharge_order.const import OrderStatus
from itpay.core.sys import export_service, const as export_const
from itpay.core.mch import service as mch_service
from itpay.core.mch import const as mch_const
from . import order_form
from .helper import wrapper_name, wrapper_agency_info


class OrderListView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('order_get'))
    @method_decorator(validate_parameters(order_form.ListOrdersSchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get('status') == OrderStatus.SUCCESS:
            cleaned_data['status'] = [OrderStatus.SUCCESS, OrderStatus.CHASE_FROZEN]
        if cleaned_data.get('export'):
            del cleaned_data['export']
            task = export_service.create_export_task(cleaned_data, export_const.ExportType.USER_RECHARGE)
            return dict(task_id=task['id'])
        c, ls = order_service.list_orders(**cleaned_data)
        return PaginationResponse(c, [wrapper_agency_info(e) for e in ls])


class InnerMchOrderListView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('inner_mch_order_get'))
    @method_decorator(validate_parameters(order_form.ListOrdersSchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get('mch_id'):
            mch = mch_service.get_mch(mch_id=cleaned_data['mch_id'])
            if mch['mch_type'] != mch_const.MchType.INNER.value:
                raise BizException(ERROR_MCH_NOT_EXISTS)
        if cleaned_data.get('status') == OrderStatus.SUCCESS:
            cleaned_data['status'] = [OrderStatus.SUCCESS, OrderStatus.CHASE_FROZEN]
        if cleaned_data.get('export'):
            del cleaned_data['export']
            task = export_service.create_export_task(cleaned_data, export_const.ExportType.USER_RECHARGE)
            return dict(task_id=task['id'])
        c, ls = order_service.list_orders(**cleaned_data, mch_type=mch_const.MchType.INNER)
        return PaginationResponse(c, [wrapper_agency_info(e) for e in ls])


class OrderDetailView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('order_get'))
    def get(self, request, order_id):
        return order_service.get_order(order_id)


class OrderChaseView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('order_chase'))
    @method_decorator(validate_parameters(order_form.ChangeOrderStatusSchema))
    def put(self, request, order_id, cleaned_data):
        order_service.chase_order(order_id, cleaned_data['remark'])
        return dict(id=order_id)


class RechargeCommentView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('comment_get'))
    @method_decorator(validate_parameters(order_form.OrderCommentSchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get('export'):
            del cleaned_data['export']
            task = export_service.create_export_task(cleaned_data, export_const.ExportType.USER_RECHARGE_COMMENT)
            return dict(task_id=task['id'])
        count, comment = recharge_comment_service.list_pay_order_comment(**cleaned_data)
        return PaginationResponse(count, [wrapper_name(e) for e in comment])
